package pri.jz.keeper.jui

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.material.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import com.google.accompanist.insets.statusBarsHeight
import pri.jz.keeper.R
import pri.jz.keeper.ui.theme.MainThemeColor
import pri.jz.keeper.ui.theme.Typography

/**
 *Author by: JZ
 *Email: 2451756617@qq.com
 *PS:
 */
@Composable
fun JTopAppBar(
    modifier: Modifier = Modifier,
    title: String = "",
    height: Dp = 56.dp,
    elevation: Dp = 4.dp,
    onFinished: () -> Unit,
    menuContent: @Composable RowScope.() -> Unit = {}
) {
    Surface(
        elevation = elevation,
        modifier = modifier.fillMaxWidth()
    ) {
        Column {
            Spacer(modifier = Modifier.fillMaxWidth().statusBarsHeight().background(MainThemeColor))
            Row(
                modifier = Modifier
                    .fillMaxWidth().height(height).background(MainThemeColor),
                verticalAlignment = Alignment.CenterVertically
            ) {
                IconButton(
                    onClick = onFinished
                ) {
                    Icon(
                        painter = painterResource(R.drawable.img_back),contentDescription = null,
                        tint = Color.Unspecified
                    )
                }
                Text(
                    text = title, style = Typography.h6,
                    modifier = Modifier.padding(horizontal = 20.dp),
                    color = Color.White
                )
                Spacer(modifier.weight(1f))
                menuContent()
            }
        }
    }
}

/**
 * 简化的顶部Bar 带有返回按钮，标题，右侧内容
 * @param onFinished
 * @param title
 * @param rightContent
 */
@Composable
fun JTopBar(
    modifier: Modifier = Modifier,
    background: Brush = MainThemeColor,
    elevation: Dp = 0.dp,
    contentColor: Color = Color.White,
    onFinished: () -> Unit,
    RightContent: @Composable RowScope.() -> Unit = {},
    Title: @Composable () -> Unit = {},
) {
    Surface(
        modifier = modifier,
        elevation = elevation,
        contentColor = contentColor
    ) {
        Column() {
            Spacer(Modifier.fillMaxWidth().statusBarsHeight().background(background))
            Row(
                modifier = Modifier.fillMaxWidth().height(30.dp).background(background),
                verticalAlignment = Alignment.CenterVertically
            ) {
                Icon(
                    painterResource(R.drawable.img_back),
                    contentDescription = null,
                    tint = Color.White,
                    modifier = Modifier
                        .padding(start = pri.jz.keeper.ui.view.HorizontalPadding)
                        .height(25.dp)
                        .clickable(onClick = onFinished)
                )
                Title()
                Spacer(modifier.weight(1f))
                RightContent()
            }
            Spacer(Modifier.fillMaxWidth().height(10.dp).background(background))
        }
    }
}

@Composable
fun JTopBar(
    modifier: Modifier = Modifier,
    background: Color = Color.White,
    elevation: Dp = 0.dp,
    contentColor: Color = Color.Black,
    onFinished: () -> Unit,
    RightContent: @Composable RowScope.() -> Unit = {},
    Title: @Composable () -> Unit = {},
) {
    Surface(
        modifier = modifier,
        elevation = elevation,
        contentColor = contentColor
    ) {
        Column{
            Spacer(Modifier.fillMaxWidth().statusBarsHeight().background(background))
            Row(
                modifier = Modifier.fillMaxWidth().height(30.dp).background(background),
                verticalAlignment = Alignment.CenterVertically
            ) {
                Icon(
                    painterResource(R.drawable.img_back),
                    contentDescription = null,
                    tint = contentColor,
                    modifier = Modifier
                        .padding(start = pri.jz.keeper.ui.view.HorizontalPadding)
                        .height(25.dp)
                        .clickable(onClick = onFinished)
                )
                Title()
                Spacer(modifier.weight(1f))
                RightContent()
            }
            Spacer(Modifier.fillMaxWidth().height(10.dp).background(background))
        }
    }
}